<script lang="ts" setup>
import { findSubCategoryFilter } from '@/api/category'
import type { FindSubCategoryFilterType } from '@/types/category'
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
const emit = defineEmits<{
  (el: 'filter-change', value: any): void
}>()
// 获取路由
const route = useRoute()
// 定义渲染数组
const filterData = ref<FindSubCategoryFilterType>()
// 定义加载状态
const loading = ref<boolean>(false)
// 路由发生变化时加载数据
watch(
  () => route.params.id,
  async (newVal) => {
    // 判断是二级分类才去获取数据
    if (newVal && `/category/sub/${route.params.id}` === route.path) {
      // 进入加载状态
      loading.value = true
      const res = await findSubCategoryFilter(route.params.id as string)
      // 数据不符合要求，改数据结构
      // 品牌添加全部
      res.result.selectedBrand = null
      res.result.brands.unshift({ id: null, name: '全部' })
      // 销售属性添加全部
      res.result.saleProperties.forEach((item) => {
        item.selectedProp = null
        item.properties.unshift({ id: null, name: '全部' })
      })
      filterData.value = res.result
      // 完成加载状态
      loading.value = false
    }
  },
  {
    immediate: true
  }
)
// 获取筛选参数
const getFilterParams = () => {
  // 定义对象存储数据
  const filterParams: any = {}
  // 定义数组存储属性
  const attrs: any = []
  // 获取当前点击的品牌数据
  filterParams.brandId = filterData.value?.selectedBrand
  filterData.value?.saleProperties.forEach((item) => {
    // 获取当前点击的属性数据
    const attr = item.properties.find(
      (i: any) => i.id === item.selectedProp
    )
    if (attr?.id) {
      // 获取成功开始提取数据
      attrs.push({ groupName: item.name, propertyName: attr.name })
    }
  })
  // 如果数组为空数组，则没必要添加了
  if (attrs.length) filterParams.attrs = attrs
  return filterParams
}
// 选择品牌触发
const changeBrand = (brandId: string) => {
  if (filterData.value?.selectedBrand === brandId) return
  filterData.value!.selectedBrand = brandId
  emit('filter-change', getFilterParams())
}
// 选择属性触发
const changeAttr = (p: any, attrId: string) => {
  if (p.selectedProp === attrId) return
  p.selectedProp = attrId
  emit('filter-change', getFilterParams())
}
</script>

<template>
  <!-- 筛选区 -->
  <div class="sub-filter" v-if="filterData && !loading">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a
          href="javasript:;"
          v-for="brand in filterData.brands"
          :key="brand.id"
          :class="{ active: filterData.selectedBrand === brand.id }"
          @click="changeBrand(brand.id)"
          >{{ brand.name }}</a
        >
      </div>
    </div>
    <div class="item" v-for="p in filterData.saleProperties" :key="p.id">
      <div class="head">{{ p.name }}：</div>
      <div class="body">
        <a
          href="javasript:;"
          v-for="attr in p.properties"
          :key="attr.id"
          :class="{ active: p.selectedProp === attr.id }"
          @click="changeAttr(p, attr.id)"
          >{{ attr.name }}</a
        >
      </div>
    </div>
  </div>
  <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
  </div>
</template>

<style scoped lang="less">
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
  .xtx-skeleton {
    padding: 10px 0;
  }
}
</style>
